<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
class tietusdk{
	private $accesskey;
	private $secretkey;
	private $base64param;
	function __construct($ak,$sk){
		if($ak == ''||$sk =='')
			return false;
		$this->accesskey = $ak;
		$this->secretkey = $sk;

	}
	public function Dealparam($param){
		$this->base64param = $this->Base64(json_encode($param));
		return $this;
	}
	public function Token(){
		$sign = $this->Base64($this->Sign($this->base64param,$this->secretkey));
		$token = $this->accesskey.':'.$sign.':'.$this->base64param;
		return $token;
	}
	public function Sign($str, $key){
		$hmac_sha1_str = "";
		if (function_exists('hash_hmac')){
			$hmac_sha1_str = $this->Base64(hash_hmac("sha1", $str, $key, true));
		} else {
			$blocksize = 64;
			$hashfunc  = 'sha1';
			if (strlen($key) > $blocksize){
				$key = pack('H*', $hashfunc($key));
			}
			$key       		= str_pad($key, $blocksize, chr(0x00));
			$ipad      		= str_repeat(chr(0x36), $blocksize);
			$opad      		= str_repeat(chr(0x5c), $blocksize);
			$hmac      		= pack('H*', $hashfunc(($key ^ $opad) . pack('H*', $hashfunc(($key ^ $ipad) . $str))));
			$hmac_sha1_str	= $this->Base64($hmac);
		}
		return $hmac_sha1_str;
	}
	public function Base64($str){
		$find = array('+', '/');
		$replace = array('-', '_');
		return str_replace($find, $replace, base64_encode($str));

	}

}
class plugin_tietuku {
	function plugin_tietuku(){
		global $_G;
		$accesskey=$_G['cache']['plugin']['tietuku']['accesskey'];
		$secretkey=$_G['cache']['plugin']['tietuku']['secretkey'];
		$album=$_G['cache']['plugin']['tietuku']['album'];
		$r_url=$_G['cache']['plugin']['tietuku']['r_url'];
		$this->shieldupload=$_G['cache']['plugin']['tietuku']['shieldupload'];
		$wailian=$_G['cache']['plugin']['tietuku']['wailian'];
		if($wailian==true){
			$wailian='true';
		}else{
			$wailian='false';
		}
		if(empty($r_url)){
			$r_url=2;
		}
		$this->wailian=$wailian;
		$this->r_url=$r_url;
		if(empty($accesskey)||empty($secretkey)||$album<1){
			$accesskey='a73ae6f4fe55ac56125c78a49033af5e17b998b5';
			$secretkey='966f69262732a26e95865490672186823de49f63';
		}else{
			$param['deadline']=time()+3600;
			$param['album']=$album;
		}
		$t=new tietusdk($accesskey,$secretkey);
		$this->token=$t->Dealparam($param)->Token();
        $param['from']='web';
        $this->urltoken=$t->Dealparam($param)->Token();
	}
}
class plugin_tietuku_forum extends plugin_tietuku {
	function post_editorctrl_left() {
		$str='<script>var token="'.$this->token.'",urltoken="'.$this->urltoken .'",wailian="'.$this->wailian .'",description="'.lang('plugin/tietuku', 'description').'",finderr="'.lang('plugin/tietuku', 'finderr').'",notabel="'.lang('plugin/tietuku', 'notabel').'",url_in="'.lang('plugin/tietuku', 'url_in').'",findimg="'.lang('plugin/tietuku', 'findimg').'",r_url="'.$this->r_url.'",up="'.lang('plugin/tietuku', 'up').'",select="'.lang('plugin/tietuku', 'select').'",uploading="'.lang('plugin/tietuku', 'uploading').'",uploadfailed="'.lang('plugin/tietuku', 'uploadfailed').'";function tietuku_box_c(){var box=document.getElementById("tietuku_box");if(box.style.display=="block")box.style.display="none"; else box.style.display="block";}</script><div id="tietuku_box" style="display:none;box-shadow: rgb(51, 51, 51) 0px 0px 10px; position: fixed; top: 0px; right: 0px; z-index: 1000000; font-family: arial, sans-serif; padding: 0px; margin: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border: 3px solid rgb(204, 204, 204); background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial;"><div onclick="tietuku_box_c()" style="width: 40px; box-shadow: rgb(51, 51, 51) 0px 0px 2px; position: absolute; top: 0px; left: -47px; line-height: 25px; border: 2px solid rgb(204, 204, 204); padding: 0px; margin: -2px 0px 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; background-color: #090; z-index: 99999; text-align: center; color: #fff; cursor: pointer; background-position: initial initial; background-repeat: initial initial;">'.lang('plugin/tietuku', 'close').' </div><iframe width="500" height="320" src="./source/plugin/tietuku/template/up.htm?v=1.1.1" style="border: none;"></iframe></div><a class="tietuku" href="javascript:tietuku_box_c();">'.lang('plugin/tietuku', 'picture').'</a>';
		if($this->shieldupload==true){
			$str.='<style type="text/css">#e_image{display:none;} .edt .b1r .tietuku {background-position: -43px -80px;} .edt .b2r .tietuku {background-position: 0 -20px;}</style>';
		}else{
			$str.="<style type=\"text/css\">.edt .b1r .tietuku {background:url('./source/plugin/tietuku/tietuku.gif') no-repeat 4px 2px;} .edt .b2r .tietuku {background:url('./source/plugin/tietuku/tietuku_1.gif') no-repeat 4px 2px;}</style>";
		}
		return $str;
	}
}
?>